library(tidyverse)
fish <- read.csv("../lab_assignments/lab7/BlackfootFish.csv")Challenge 7: Incorporating Multiple Inputs
Functions + Fish
This is a continuation of Lab 7: Functions + Fish.
A frequently used measurement for fish health is a condition index (Wikipedia article). The following simple equation can be used to calculate the approximate condition index of a fish:
\[\text{condition index} = \frac{weight}{length^3} \times 100\]
1. There are specific units required for the calculation of a condition index – length must be in millimeters and weight must be in grams. Inspect the length and weight variables to decide if you believe these are the correct units associated with the measurements in our dataset. If they are not, convert the measurements.
This will likely require Googling what “typical” measurements of trout are.
# Question 1 code2. Replace impossible measurements with NAs, using your research to determine what measurements are unlikely or impossible. Write a function(s) to handle this process.
Your function(s) should accept three inputs
- a vector of measurements,
- the minimum value you believe is “reasonable,”
- the maximum value you believe is “reasonable.”
If a value falls outside these bounds, you should replace it with an NA.
If you are struggling with the structure of your function, I would suggest reading the Mutating Function from R4DS.
Use your function to modify the length and weight columns of the BlackfootFish data set, removing values you believe are unreasonable.
# Question 2 code3. Write a function to calculate the condition index of a fish, given inputs of weight and length.
Consider whether your function will accept vectors as inputs or if it will accept variable names as inputs!
# Question 3 code4. Make a thoughtful visualization of how fish conditions have varied over the duration of this study.
# Question 4 code